package Raid;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.Image;
import java.util.ArrayList;;


public class Environment {

	Block[][] blocks;
	
	ArrayList<Baddy> baddies;
	
	Image heightMap;
	int levelX = 200;
	
	int orbX = 10;
	int orbY = 20;
	
	int windowWidth;
	int windowHeight;
	
	int resX;
	int resY;
	
	
	public Environment(int windowWidth, int windowHeight, int resX, int resY, Image hMap){
		Block.width = windowWidth/resX;
		heightMap = hMap;
		this.windowWidth = windowWidth;
		this.windowHeight = windowHeight;
		this.resX = resX;
		this.resY = resY;
		baddies = new ArrayList<Baddy>();
		for (int i = 0; i < 2; i++){
			baddies.add(new Squirm(this, i *50, 0));
		}
		for (int i = 0; i < 1; i++){
			baddies.add(new Parasquid(this, 300, 0));
		}
		blocks = initBlocks();
		calcInitialTextures();
		changeBlock(orbX, orbY, new Orb(orbX*Block.width, orbY*Block.width));
	}
	
	public void tick(int delta){
		for (Baddy b: baddies){
			b.doLogic(delta);
		}
	}
	
	
	public Block pointToBlock(double x, double y){
		try{
			return blocks[(int)(x/Block.width)][(int)(y/Block.width)];
		} catch (ArrayIndexOutOfBoundsException e){
			return null;
		}
	}
	
	
	public Block[] getSurroundingBlocks(double x, double y, double radius){
		Block[] surroundings = {pointToBlock(x-radius, y-radius), pointToBlock(x, y-radius), pointToBlock(x+radius, y-radius),
								pointToBlock(x-radius, y), pointToBlock(x+radius, y), pointToBlock(x-radius, y+radius),
								pointToBlock(x, y+radius), pointToBlock(x+radius, y+radius)};
		return surroundings;
	}
	
	public Block[] getBeneathBlocks(double x, double y, double radius){
		Block[] surroundings = {pointToBlock(x-radius, y+radius), pointToBlock(x, y+radius), pointToBlock(x+radius, y+radius)};
		return surroundings;
	}
	
	public Block[] getAboveBlocks(double x, double y, double radius){
		Block[] surroundings = {pointToBlock(x-radius, y-radius), pointToBlock(x, y-radius), pointToBlock(x+radius, y-radius),
								pointToBlock(x-radius, y), pointToBlock(x, y), pointToBlock(x+radius, y)};
		return surroundings;
	}
	
	public Block[] getSideBlocks(double x, double y, double radius){
		Block[] surroundings = {pointToBlock(x-radius, y-radius), pointToBlock(x+radius, y-radius),
				pointToBlock(x-radius, y), pointToBlock(x+radius, y), pointToBlock(x-radius, y+radius), pointToBlock(x+radius, y+radius)};
		return surroundings;
	}
	
	public void changeBlock(int x, int y, Block block){
		blocks[x][y] = block;
	}
	
	
	
	
	public void render(){
		for (int i = 0; i < blocks.length; i++){
			for (int j = 0; j < blocks[0].length; j++){
				blocks[i][j].render();
			}
		}
		
		for (Baddy b: baddies){
			b.render();
		}
	}
	
	
	public Block[][] initBlocks(){
		Block[][] arr = new Block[resX][resY];
		for (int i = 0; i < resX; i++){
			int h = (int)((heightMap.getColor(levelX%heightMap.getWidth(), levelX/heightMap.getWidth()).r + .5)/2.0 * resY);
			for (int j = 0; j < resY; j++){
				if (j < h){
					Air a = new Air(i*Block.width, j*Block.width);
					//a.texture = blockTextures[a.id.ordinal()];
					arr[i][j] = a;
				} else{
					Grass b = new Grass(i*Block.width, j*Block.width);
					b.texture = Grass.textures[0];
					arr[i][j] = b;
				}
			}
			levelX++;
		}
		return arr;
	}
	
	
	public boolean damageBlock(double x, double y, double damage){
		Block b = this.pointToBlock(x, y);
		if (b.takeDamage(damage) == true){
			changeBlock((int)(x/Block.width), (int)(y/Block.width), new Air(x, y));
			return true;
		} else
			return false;
	}
	
	
	
	
	public void calcInitialTextures(){
		for (int i = 0; i < resX; i++){
			for (int j = 0; j < resY; j++){
				Block b = blocks[i][j];
				if (b.id == Type.GRASS){
					Block a = pointToBlock(b.x, b.y - Block.width);
					if (a == null || a.id == Type.AIR){
						b.texture = Grass.textures[0];
					} else{
						b.texture = Grass.textures[5];
					}
					
				}
			}
			
		}
	}
	
	
}
